package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;


    @GetMapping
    public Result list(Integer subject,Integer target,
                       @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("列表查询；" + subject + " " + target + " " + page + " " + pageSize);
        PageResult<Course> pageResult = courseService.list(subject, target, page, pageSize);
        return Result.success(pageResult);

    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除部门：" + id);
        courseService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("添加部门：" + course);
        courseService.add(course);
        return Result.success();
    }
    @GetMapping ("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("获取部门：" + id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("更新部门：" + course);
        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(){
        log.info("获取所有部门");
        return Result.success(courseService.listAll());
    }
    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject){
        log.info("根据学科查询课程: {}", subject);
        List<Course> course = courseService.listBySubject(subject);
        return Result.success(course);
    }

}
